package com.yuxin.tongyi.lsll.common;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.util.Log;
import android.widget.Toast;

/**
 * @author daijin'ge
 * 2012-4-16
 * 监听网络状态
 */

public class NetworkStateBroadcast extends BroadcastReceiver {
	@Override
	public void onReceive(Context context, Intent arg1) {

		boolean success = false;
		
		 //获得网络连接服务
		 ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);
		 State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState(); // 获取网络连接状态
		 if (State.CONNECTED == state) { // 判断是否正在使用WIFI网络
			 success = true;
		 }
		 state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState(); // 获取网络连接状态
		 if (State.CONNECTED == state) { // 判断是否正在使用GPRS网络
			 success = true;
		 }
		 state = connManager.getNetworkInfo(ConnectivityManager.DEFAULT_NETWORK_PREFERENCE).getState(); // 获取网络连接状态
		 if (State.CONNECTED == state) { // 判断是否正在使用互联网络
			 success = true;
		 }
		 if (!success) {
			 Toast.makeText(context, "网络连接已中断", Toast.LENGTH_LONG).show();
		 } 
	}

}
